package kz.kstu.aec.util;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

/**
 * @author Filipp_Stankevich
 *         created: 23.01.12 18:57
 */
public class CryptographyUtil {

    private static final String DIGEST_ALGORITHM = "SHA-1";
    private static final String ENCODING = "UTF-8";

    private static Random random = new Random();

    private CryptographyUtil() {
    }

    public static byte[] getHash(String inputString) {
        try {
            return getHash(inputString.getBytes(ENCODING));
        } catch (UnsupportedEncodingException e) {
            throw new IllegalArgumentException("encoding not supported");
        }
    }

    public static byte[] getHash(byte[] data) {
        try {
            MessageDigest digest = MessageDigest.getInstance(DIGEST_ALGORITHM);
            return digest.digest(data);
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException("digest algorithm was not found");
        }
    }

    public static BigInteger getRandomNumber() {
        return BigInteger.valueOf(random.nextInt());
    }
}
